package com.byedu.course.controller;

import com.byedu.course.AdsService;
import com.byedu.course.GuideInforService;
import com.byedu.course.JobInforService;
import com.byedu.domain.course.AdsDO;
import com.byedu.domain.course.GuideInforDO;
import com.byedu.domain.course.JobInforDO;
import com.byedu.domain.course.vo.GuideInforVO;
import com.byedu.domain.course.vo.JobInforVO;
import com.byedu.util.CommonUtil;
import com.byedu.util.FileUploadUtil;
import com.byedu.util.page.PageInfo;
import com.byedu.util.page.PageUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * Created by Luo on 2017-06-28.
 */
@Controller
@RequestMapping("/ads")
public class AdminAdsController {

    @Autowired
    private AdsService adsService;

    @Autowired
    private GuideInforService guideInforService;

    @Autowired
    private JobInforService jobInforService;

    @RequestMapping("index")
    public String defaultr(HttpServletRequest request) {
        String m = request.getParameter("m");
        if (StringUtils.isEmpty(m)) {
            m = "ads";
        }
        request.setAttribute("m", m);
        request.setAttribute("newsnum", 100);
        request.setAttribute("clazznum", 20);
        request.setAttribute("sourcenum", 30);
        request.setAttribute("topicnum", 50);
        return "index";
    }


    @RequestMapping("/adsList")
    public String adsList(HttpServletRequest request,@RequestParam(value = "page", required = false) String pageStr) {
        int pageInt = CommonUtil.StringToPageInt(pageStr);
        String m = request.getParameter("m");
        if ("add".equals(m)) {
            return "ads/addads";
        }
        if ("edit".equals(m)) {
//            CommonUtil.StringToInt(request.getParameter("id"));
            int id = CommonUtil.StringToInt(request.getParameter("id"));

            AdsDO adsDO = adsService.getAdsById(id);
            request.setAttribute("ads", adsDO);
            return "ads/addads";
        }
        if ("list".equals(m)) {
            Page page = PageHelper.startPage(pageInt, 5);
            List<AdsDO> adsVOList = adsService.listAds();
            request.setAttribute("adsList", adsVOList);
//            for(AdsDO adsDO: adsVOList ){
//                System.out.print("---"+adsDO.toString());
//            }
            System.out.println("page.size---" + page.size());
            System.out.println("page.getPageNum---" + page.getPageNum());
            System.out.println("page.getPages---" + page.getPages());
            System.out.println("page.getOrderBy---" + page.getOrderBy());
//            System.out.print("page.getPageNum---"+page.get);
            System.out.println(" page.getTotal()---" + page.getTotal());
            //分页
            PageInfo pi=new PageInfo("/ads/adsList?m=list&page=",page.getTotal(),page.getPageNum(),5);
            request.setAttribute("PageInfo", pi);
            return "ads/adslist";
        }
        return "ads/adslist";
    }

    @RequestMapping("/jobinfoList")
    public String jobinfoList(HttpServletRequest request) {

        String m = request.getParameter("m");

        if ("edit".equals(m)) {
            int id = CommonUtil.StringToInt(request.getParameter("id"));
            JobInforDO jobInforDO = jobInforService.getJobInforbyId(id);
            request.setAttribute("jobInfo", jobInforDO);
            return "ads/addjobinfo";
        }
        if ("add".equals(m)) {
            return "ads/addjobinfo";
        }
        if ("list".equals(m)) {
            List<JobInforVO> jobInforVOList = jobInforService.getJobInforLists();
            request.setAttribute("jobList", jobInforVOList);
            //分页
            PageUtil.save(request);
            return "ads/jobinfolist";
        }
        return "ads/jobinfolist";
    }

    @RequestMapping("/jobinfoadd")
    public String jobinfoadd(@RequestParam(value = "name", required = false) String name,
                             @RequestParam(value = "start_time", required = false) String start_time,
                             @RequestParam(value = "end_time", required = false) String end_time,
                             @RequestParam(value = "url", required = false) String url,
                             @RequestParam(value = "intro", required = false) String intro,
                             @RequestParam(value = "city", required = false) String city,
                             @RequestParam(value = "top", required = false) String top,
                             HttpServletRequest request, HttpServletResponse response) {

        String m = request.getParameter("m");
        if ("dopub".equals(m)) {

            String img = new FileUploadUtil().saveOneFile(request, "jobinfo");
            if (img == null) {
                CommonUtil.outpostprint(response, "上传文件出错");
                return null;
            }
            try {
                JobInforDO jobInforDO = new JobInforDO();
                jobInforDO.setName(name);
                jobInforDO.setStart_time(start_time);
                jobInforDO.setEnd_time(end_time);
                jobInforDO.setUrl(url);
                jobInforDO.setIntro(intro);
                jobInforDO.setCity(city);
                jobInforDO.setTop("true".equals(top) ? 1 : 0);
                jobInforDO.setImg(img);
                int num = jobInforService.saveJobInfor(jobInforDO);
                if (num > 0) {
                    response.sendRedirect(request.getContextPath() + "/ads/jobinfoList?m=list");
                } else {
                    CommonUtil.outpostprint(response, "数据存储错误");
                }

            } catch (IOException e) {
                e.printStackTrace();
            }
            return null;
        }
        if ("doedit".equals(m)) {
            int id = CommonUtil.StringToInt(request.getParameter("id"));
            String img = new FileUploadUtil().saveOneFile(request, "jobinfo");
            if (img == null) {
                CommonUtil.outpostprint(response, "上传文件出错");
                return null;
            }
            try {
                JobInforDO jobInforDO = new JobInforDO();
                jobInforDO.setId(id);
                jobInforDO.setName(name);
                jobInforDO.setStart_time(start_time);
                jobInforDO.setEnd_time(end_time);
                jobInforDO.setUrl(url);
                jobInforDO.setIntro(intro);
                jobInforDO.setCity(city);
                jobInforDO.setTop("true".equals(top) ? 1 : 0);
                jobInforDO.setImg(img);
                int num = jobInforService.updateJobInfor(jobInforDO);
                if (num > 0) {
                    response.sendRedirect(request.getContextPath() + "/ads/jobinfoList?m=list");
                } else {
                    CommonUtil.outpostprint(response, "数据存储错误");
                }

            } catch (IOException e) {
                e.printStackTrace();
            }
            return null;
        }
        return "ads/addjobinfo";
    }

    @RequestMapping("/guideinfoadd")
    public String guideinfoadd(@RequestParam(value = "name", required = false) String name,
                               @RequestParam(value = "start_time", required = false) String start_time,
                               @RequestParam(value = "end_time", required = false) String end_time,
                               @RequestParam(value = "url", required = false) String url,
                               @RequestParam(value = "intro", required = false) String intro,
                               @RequestParam(value = "top", required = false) String top,
                               HttpServletRequest request, HttpServletResponse response) {

        String m = request.getParameter("m");
        if ("dopub".equals(m)) {

            String img = new FileUploadUtil().saveOneFile(request, "guideinfo");
            if (img == null) {
                CommonUtil.outpostprint(response, "上传文件出错");
                return null;
            }
            try {
                GuideInforDO guideInforDO = new GuideInforDO();
                guideInforDO.setName(name);
                guideInforDO.setStart_time(start_time);
                guideInforDO.setEnd_time(end_time);
                guideInforDO.setUrl(url);
                guideInforDO.setIntro(intro);
                System.out.println("top:" + top);
                guideInforDO.setTop("true".equals(top) ? 1 : 0);
                guideInforDO.setImg(img);
                int num = guideInforService.saveGuideInfor(guideInforDO);
                if (num > 0) {
                    response.sendRedirect(request.getContextPath() + "/ads/guideinfoList?m=list");
                } else {
                    CommonUtil.outpostprint(response, "数据存储错误");
                }

            } catch (IOException e) {
                e.printStackTrace();
            }
            return null;
        }
        if ("doedit".equals(m)) {
            int id = CommonUtil.StringToInt(request.getParameter("id"));
            String img = new FileUploadUtil().saveOneFile(request, "guideinfo");
            try {
                GuideInforDO guideInforDO = new GuideInforDO();
                guideInforDO.setId(id);
                guideInforDO.setName(name);
                guideInforDO.setStart_time(start_time);
                guideInforDO.setEnd_time(end_time);
                guideInforDO.setUrl(url);
                guideInforDO.setIntro(intro);
                guideInforDO.setTop("true".equals(top) ? 1 : 0);
                guideInforDO.setImg(img);
                int num = guideInforService.updateGuideInfor(guideInforDO);
                if (num > 0) {
                    response.sendRedirect(request.getContextPath() + "/ads/guideinfoList?m=list");
                } else {
                    CommonUtil.outpostprint(response, "数据存储错误");
                }

            } catch (IOException e) {
                e.printStackTrace();
            }
            return null;
        }
        return "ads/addguideinfo";
    }


    @RequestMapping("/guideinfoList")
    public String clazzList(HttpServletRequest request) {

        String m = request.getParameter("m");
        if ("add".equals(m)) {
            return "ads/addguideinfo";
        }
        if ("edit".equals(m)) {
            int id = CommonUtil.StringToInt(request.getParameter("id"));
            GuideInforDO guideInforDO = guideInforService.getGuideInforbyId(id);
            request.setAttribute("guideInfo", guideInforDO);
            return "ads/addguideinfo";
        }

        if ("list".equals(m)) {
            List<GuideInforVO> guideInforVOList = guideInforService.getGuideInforLists();
            request.setAttribute("guideList", guideInforVOList);
            //分页
            PageUtil.save(request);
            return "ads/guideinfolist";
        }
        return "ads/guideinfolist";
    }

    @RequestMapping("/adsadd")
    public String adsadd(@RequestParam(value = "name", required = false) String name,
                         @RequestParam(value = "ordersort", required = false) String ordersort,
                         @RequestParam(value = "enable", required = false) String enable,
                         @RequestParam(value = "dictUrl", required = false) String dictUrl,
                         @RequestParam(value = "startTime", required = false) String startTime,
                         @RequestParam(value = "endTime", required = false) String endTime,
                         HttpServletRequest request, HttpServletResponse response) {

        String m = request.getParameter("m");
        if ("dopub".equals(m)) {

            String img = new FileUploadUtil().saveOneFile(request, "ads");
            if (img == null) {
                CommonUtil.outpostprint(response, "上传文件出错");
                return null;
            }
            try {
                AdsDO adsDO = new AdsDO();
                adsDO.setName(name);
                adsDO.setDictUrl(dictUrl);
                adsDO.setEnable(Boolean.parseBoolean(enable));
                adsDO.setImg(img);
                adsDO.setOrdersort(Integer.parseInt(ordersort));
                adsDO.setStartTime(startTime);
                adsDO.setEndTime(endTime);
                int num = adsService.saveAds(adsDO);
                if (num > 0) {
                    response.sendRedirect(request.getContextPath() + "/ads/adsList?m=list");
                } else {
                    CommonUtil.outpostprint(response, "数据存储错误");
                }

            } catch (IOException e) {
                e.printStackTrace();
            }
            return null;
        }
        if ("doedit".equals(m)) {
            int id = CommonUtil.StringToInt(request.getParameter("id"));
            String img = new FileUploadUtil().saveOneFile(request, "ads");
            if (img == null) {
                CommonUtil.outpostprint(response, "上传文件出错");
                return null;
            }
            try {
                AdsDO adsDO = new AdsDO();
                adsDO.setId(id);
                adsDO.setName(name);
                adsDO.setDictUrl(dictUrl);
                adsDO.setEnable(Boolean.parseBoolean(enable));
                adsDO.setImg(img);
                adsDO.setOrdersort(Integer.parseInt(ordersort));
                adsDO.setStartTime(startTime);
                adsDO.setEndTime(endTime);
                int num = adsService.updateAds(adsDO);
                if (num > 0) {
                    response.sendRedirect(request.getContextPath() + "/ads/adsList?m=list");
                } else {
                    CommonUtil.outpostprint(response, "数据存储错误");
                }

            } catch (IOException e) {
                e.printStackTrace();
            }
            return null;
        }
        return "ads/addads";
    }

    @RequestMapping("/adsLibraryedit")
    public String clazzedit(HttpServletRequest request, HttpServletResponse response) {
//        int id = CommonUtil.StringToInt(request.getParameter("id"));
//        String content = request.getParameter("content");
//        if(id!=0){
//            QuestionVO entity = clazzMenuDao.selectById(ClazzMenuEntity.class, id);
//            if(entity!=null){
//                entity.setName(co);
//                clazzMenuDao.update_(entity);
//                response.getWriter().write("1");
//            }
//        }
        try {
            response.getWriter().write("1");
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }


    @RequestMapping("/adsAdd")
    public String clazzAdd(HttpServletRequest request, HttpServletResponse response) {

//        ClazzForm clazzForm = (ClazzForm)form;
//        ClazzEntity entity = new ClazzEntity();
//        BeanUtils.copyProperties(clazzForm, entity);
//        FormFile imgFile = clazzForm.getImgFile();
//        if(imgFile!=null&&imgFile.getFileSize()>0){
//            UploadFileImpl uploadFile = new UploadFileImpl(path+"/images",filesize,imgType,imgFile);
//            uploadFile.save(DateUtil.getDateString());
//            entity.setImg(uploadFile.getUploadFileName());
//        }
//        FormFile file = clazzForm.getFile();
//        if(file!=null&&file.getFileSize()>0){
//            UploadFileImpl uploadFile = new UploadFileImpl(path+"/files",filesize,type,file);
//            uploadFile.save(DateUtil.getDateString());
//            entity.setFilename(uploadFile.getUploadFileName());
//        }
//        FormFile flashFile = clazzForm.getFlashFile();
//        if(flashFile!=null&&flashFile.getFileSize()>0){
//            UploadFileImpl uploadFile = new UploadFileImpl(path+"/files",filesize,flashType,flashFile);
//            uploadFile.save(DateUtil.getDateString());
//            entity.setFlashFilename(uploadFile.getUploadFileName());
//        }
//        ClazzMenuEntity menu = clazzMenuDao.selectById(ClazzMenuEntity.class, clazzForm.getMenuId());
//        if(menu!=null){
//            entity.setClazzMenu(menu);
//        }
//        FileTypeEntity fileType = fileTypeDao.selectById(FileTypeEntity.class, clazzForm.getFiletypeId());
//        if(fileType!=null){
//            entity.setFileType(fileType);
//        }
//        clazzDao.save_(entity);
//        response.sendRedirect(request.getContextPath()+"/admin/clazz/clazz.do?m=list");
        return null;
    }


    @RequestMapping("/adsdel")
    public String adsdel(@RequestParam(value = "id", required = false) int id,
                         HttpServletRequest request, HttpServletResponse response) {
//删除图片 要做
//        String suffix = fileName.substring(fileName.lastIndexOf('.'));

        try {
            adsService.delAds(id);
            response.sendRedirect(request.getContextPath() + "/ads/adsList?m=list");
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }


}
